package com.chen.config;

import cn.hutool.core.io.FileUtil;
import com.chen.handler.LoginInterceptor;
import com.chen.util.PathUtils;
import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @Author:chenao
 * @DATE:2022/3/12 17:40
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String uploadPath = PathUtils.getUploadPath();
        //格式为"file:C:/Users/app-fitness/app-admin/target/upload/"
        String location="file:"+uploadPath;
        String property = System.getProperties().getProperty("os.name");
        if (property.contains("Windows")) {
            //Windows下
            registry.addResourceHandler("/img/**").addResourceLocations(location);
        } else{
            //Linux系统
            registry.addResourceHandler("/img/**").addResourceLocations(location);
        }
        super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截test接口，后续实际遇到需要拦截的接口时，在配置为真正的拦截接口
        registry.addInterceptor(loginInterceptor).
                addPathPatterns("/sysUser/**").
                addPathPatterns("/user/**");
    }
}
